package com.lifeng.fastssmr.dao.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.util.DBObjectUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.lifeng.fastssmr.base.impl.BaseDaoImpl;
import com.lifeng.fastssmr.dao.ItemDao;
import com.lifeng.fastssmr.pojo.Item;
import com.lifeng.fastssmr.web.controller.ItemController;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
@Repository
public class ItemDaoImpl extends BaseDaoImpl<Item> implements ItemDao{
	private static Logger logger = Logger.getLogger(ItemController.class);
	@Override
	public List<String> saveFiles(String itemId,MultipartFile[] picFile) throws Exception {
		List<String> fileIds =  new ArrayList<String>();
		for (MultipartFile multipartFile : picFile) {
			String contentType = multipartFile.getContentType();
			InputStream in  = multipartFile.getInputStream();
			String oriName = multipartFile.getOriginalFilename();//原始文件名
			if(!StringUtils.isEmpty(oriName)){//判断空文件
				String fileName = multipartFile.getName();
				long fileSize = multipartFile.getSize();
				logger.info("contentType:"+contentType+",name:"+fileName+",fileName:"+oriName+",fileSize:"+fileSize);
				DBObject metaData = new BasicDBObject();
				metaData.put("itemId", itemId);
				try {
					String fileId = saveFile(in, oriName, contentType, metaData);
					fileIds.add(fileId);
				} catch (Exception e) {
					e.printStackTrace();
					throw new Exception("保存文件",e);
				}
			}
		}
		return fileIds;
	}
	
}
